package com.example.ViewPagerSum;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import com.example.ViewPagerSum.adapter.HomeFragmentAdapter;
import com.example.ViewPagerSum.fragment.DiscoverFragment;
import com.example.ViewPagerSum.fragment.HomeFragment;
import com.example.ViewPagerSum.fragment.MeFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener{

    private ViewPager viewpager;
    private RadioGroup tab_bar;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        viewpager= (ViewPager) findViewById(R.id.viewpager);
        tab_bar= (RadioGroup) findViewById(R.id.tab_bar);
        tab_bar.setOnCheckedChangeListener(this);
        List<Fragment> list=new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new DiscoverFragment());
        list.add(new MeFragment());
        FragmentPagerAdapter  adapter=new HomeFragmentAdapter(getSupportFragmentManager(),list);
        viewpager.setAdapter(adapter);
        viewpager.setOnPageChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.tab_home:
                viewpager.setCurrentItem(0);
                break;
            case R.id.tab_discover:
                viewpager.setCurrentItem(1);
                break;
            case R.id.tab_me:
                viewpager.setCurrentItem(2);
                break;
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int index) {
        int id=-1;
        switch (index){
            case 0:
                id=R.id.tab_home;
                break;
            case 1:
                id=R.id.tab_discover;
                break;
            case 2:
                id=R.id.tab_me;
                break;
        }
        tab_bar.check(id);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
